import 'package:common/base/base_lv_adapter.dart';
import 'package:common/base/base_lv_state.dart';
import 'package:common/base/base_stateful_widget.dart';
import 'package:common/constant/constant.dart';
import 'package:common/mvp/model/node_bean.dart';
import 'package:common/utils/tree_helper.dart';
import 'package:flutter/material.dart';
import 'package:toast/toast.dart';

import '../present/home_present.dart';

class TreeListPage extends BaseStatefulWidget {
  const TreeListPage({super.key});

  @override
  State<TreeListPage> createState() => _TreeListPageState();
}

class _TreeListPageState
    extends BaseLvState<TreeListPage, NodeBean<dynamic>> {

  late HomePresent _homePresent;

  final TreeHelper<dynamic> _treeHelper = TreeHelper<dynamic>();

  final List<NodeBean<dynamic>> _treeDataList = [];

  late List<NodeBean<dynamic>> _visibleNodes;

  @override
  void onStateInit() {
    super.onStateInit();
    _homePresent = HomePresent(this);
  }

  @override
  bool onNeedAppBar() {
    return false;
  }

  @override
  BaseLvAdapter<NodeBean<dynamic>> onInitAdapter() {
    return BaseLvAdapter<NodeBean<dynamic>>((NodeBean<dynamic> t, int index) {
      double leftMargin = t.level! * 10;
      return Container(
          height: 30,
          margin: EdgeInsets.fromLTRB(leftMargin, 10, 5, 0),
          decoration:
              BoxDecoration(color: Colors.grey[100], borderRadius: BorderRadius.circular(5)),
          child: InkWell(
              onTap: () {
                if (t.isLeaf()) {
                  Toast.show("this is leaf", duration: Toast.lengthLong, gravity: Toast.bottom);
                } else {
                  t.setExpand(!t.isExpand);
                  _visibleNodes = _treeHelper.filterVisibleNode(_treeDataList, index);
                  refreshOrLoadSuccess(_visibleNodes);
                }
              },
              child: Row(
                children: [
                  Visibility(
                      maintainState: true,
                      maintainAnimation: true,
                      maintainSize: true,
                      visible: !t.isLeaf(),
                      child: t.isExpand ? Icon(
                        t.iconExpand,
                        size: 20,
                        color: Colors.blue,
                      ) : Icon(
                        t.iconCollapse,
                        size: 15,
                        color: Colors.purple,
                      )),
                  Text(t.name ?? "")
                ],
              )));
    });
  }

  //创建ListView的Item,并且绑定数据

  @override
  void onLoadData() {
    showDelayLoadingDialog(context);
    Future.delayed(const Duration(seconds: 3), () {
      _homePresent.getTreeList();
    });
  }

  @override
  Future<void> onPullRefresh() async {
    super.onPullRefresh();
    await Future.delayed(const Duration(seconds: 3), () {});
  }

  @override
  Future<void> onPullLoadMore() async {
    super.onPullLoadMore();
    await Future.delayed(const Duration(seconds: 3), () {});
  }

  @override
  void onSuccess<D>(D d , String apiPath) {
    super.onSuccess(d, apiPath);
    if(d is NodeBean<dynamic>) {
      NodeBean<dynamic> rootNode = d;
      _treeHelper.convertTreeData2List(_treeDataList,rootNode,Constant.zero,false);
      _visibleNodes = _treeHelper.filterVisibleNode(_treeDataList, Constant.zero);
      refreshOrLoadSuccess(_visibleNodes);
    }
  }
}
